package com.me.launcher.activity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.me.launcher.R;
import com.me.launcher.tools.ScreenUtils;
import com.me.launcher.tools.Tools;


/**
 * @author hejiangwei
 * @date 2018/3/30
 */
public class SearchAppFragment extends Fragment implements View.OnClickListener {
//    private static final String ARG_PARAM1 = "param1";
//    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private EditText mEditText;
    private RelativeLayout mrlBgSearch;
    private OnSearchAppListener mListener;
    private ImageButton mButtonSearch, mButtonClear;
    private GridView mgvSearch;

    public SearchAppFragment() {
        // Required empty public constructor
    }

    public static SearchAppFragment newInstance() {//String param1, String param2
        SearchAppFragment fragment = new SearchAppFragment();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
//            mParam1 = getArguments().getString(ARG_PARAM1);
//            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_search_app, container, false);
        mEditText = rootView.findViewById(R.id.et_search);
        mgvSearch = rootView.findViewById(R.id.gv_searchApp);
        mrlBgSearch = rootView.findViewById(R.id.search_bg);
        mButtonSearch = rootView.findViewById(R.id.btn_search);
        mButtonClear = rootView.findViewById(R.id.search_clear);

        rootView.setOnClickListener(this);
        mEditText.setHorizontallyScrolling(true);
        mEditText.setOnFocusChangeListener((view, b) -> mrlBgSearch.setSelected(b));
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mButtonClear.setVisibility(TextUtils.isEmpty(editable.toString()) ? View.GONE : View.VISIBLE);
            }
        });
        mButtonClear.setOnClickListener(this);
        mButtonSearch.setOnClickListener(this);
        mEditText.setOnClickListener(this);
        mEditText.setOnFocusChangeListener((view, b) -> {
           if(!b) {
               mListener.onSearchFinish();
           }
        });
        if (ScreenUtils.getInstance().getScreenW()> ScreenUtils.getInstance().getScreenH()){
            Tools.setViewWidth(mrlBgSearch, (int) (ScreenUtils.getInstance().getScreenW()/1.8));
        }
        return rootView;

    }



    private void hideInput(View view) {
        mEditText.setFocusable(false);
        Tools.HideInput(view);
    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onSearchApp(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnSearchAppListener) {
            mListener = (OnSearchAppListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_search:
                hideInput(mEditText);
                break;
            case R.id.et_search:
                Tools.getRequest(mEditText);
                break;
            case R.id.search_clear:
                mEditText.getText().clear();
                break;

            case R.id.ll_content:
                hideInput(mEditText);
                break;
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnSearchAppListener {
        // TODO: Update argument type and name
        void onSearchApp(Uri uri);
        void onSearchFinish();
    }
}
